<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>粘性初测实验抽样单</title>
    <link rel="stylesheet" href="./css/all.min.css">
    <script src="unit-codes.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', 'Microsoft YaHei', sans-serif;
        }

        body {
            background: linear-gradient(-45deg, #667eea, #764ba2, #f093fb, #f5576c);
            background-size: 400% 400%;
            animation: gradientShift 15s ease infinite;
            min-height: 100vh;
            color: #333;
            line-height: 1.6;
        }

        @keyframes gradientShift {
            0% { background-position: 0% 50%; }
            50% { background-position: 100% 50%; }
            100% { background-position: 0% 50%; }
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
        }

        .header {
            display: flex;
            align-items: center;
            justify-content: space-between;
            margin-bottom: 30px;
            background: rgba(255, 255, 255, 0.1);
            backdrop-filter: blur(10px);
            border-radius: 15px;
            padding: 20px;
            box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
        }

        .back-btn {
            background: rgba(255, 255, 255, 0.2);
            border: 1px solid rgba(255, 255, 255, 0.3);
            color: white;
            padding: 10px 20px;
            border-radius: 25px;
            cursor: pointer;
            font-size: 14px;
            transition: all 0.3s ease;
            text-decoration: none;
            display: inline-flex;
            align-items: center;
            gap: 8px;
        }

        .back-btn:hover {
            background: rgba(255, 255, 255, 0.3);
            transform: translateY(-2px);
        }

        .header h1 {
            color: white;
            font-size: 28px;
            font-weight: 600;
            text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3);
        }

        .barcode-input-section {
            background: white;
            border-radius: 15px;
            padding: 25px;
            margin-bottom: 30px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
            animation: fadeInUp 0.6s ease forwards;
        }

        @keyframes fadeInUp {
            from {
                opacity: 0;
                transform: translateY(30px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }

        .barcode-input-row {
            display: flex;
            align-items: center;
            gap: 15px;
            flex-wrap: wrap;
        }

        .barcode-label {
            font-weight: 600;
            color: #2c3e50;
            min-width: 120px;
        }

        .barcode-input {
            flex: 1;
            min-width: 300px;
            padding: 12px 15px;
            border: 2px solid #e1e8ed;
            border-radius: 8px;
            font-size: 14px;
            transition: all 0.3s ease;
        }

        .barcode-input:focus {
            outline: none;
            border-color: #667eea;
            box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
        }

        .barcode-btn {
            padding: 12px 20px;
            border: none;
            border-radius: 8px;
            cursor: pointer;
            font-size: 14px;
            font-weight: 600;
            transition: all 0.3s ease;
            display: inline-flex;
            align-items: center;
            gap: 8px;
            min-width: 100px;
            justify-content: center;
        }

        .barcode-btn:not(.secondary) {
            background: linear-gradient(135deg, #667eea, #764ba2);
            color: white;
        }

        .barcode-btn.secondary {
            background: linear-gradient(135deg, #4ecdc4, #44a08d);
            color: white;
        }

        .barcode-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
        }

        .form-container {
            background: white;
            border-radius: 15px;
            padding: 30px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
            animation: fadeInUp 0.6s ease forwards;
            animation-delay: 0.2s;
            width: 100%;
        }

        /* 保持a4-page类的原有样式不变 */
        .a4-page {
            font-family: SimSun, serif;
            background: white;
            padding: 20px;
        }
        
        .a4-page .title {
            text-align: center;
            font-size: 18px;
            font-weight: bold;
            margin-bottom: 30px;
        }
        
        .a4-page table {
            border-collapse: collapse;
            width: 100%;
            max-width: 1600px;
            max-height: 900px;
            margin: 0 auto;
        }
        
        .a4-page td {
            border: 1px solid black;
            padding: 8px;
            text-align: center;
            vertical-align: middle;
            height: 40px;
        }

        /* 响应式设计 */
        @media (max-width: 768px) {
            .container {
                padding: 15px;
            }

            .header {
                flex-direction: column;
                gap: 15px;
                text-align: center;
            }

            .header h1 {
                font-size: 24px;
            }

            .barcode-input-row {
                flex-direction: column;
                align-items: stretch;
            }

            .barcode-input {
                min-width: auto;
            }

            .barcode-btn {
                width: 100%;
            }
        }

        /* 加载动画 */
        @keyframes pulse {
            0% { transform: scale(1); }
            50% { transform: scale(1.05); }
            100% { transform: scale(1); }
        }

        .barcode-btn:hover {
            animation: pulse 0.6s ease-in-out;
        }

        /* 通知样式 */
        .notification-container {
            position: fixed;
            top: 20px;
            right: 20px;
            z-index: 1000;
            max-width: 350px;
        }

        .notification {
            background: white;
            border-radius: 12px;
            padding: 16px 20px;
            margin-bottom: 10px;
            box-shadow: 0 8px 32px rgba(0, 0, 0, 0.15);
            border-left: 4px solid;
            position: relative;
            animation: slideInRight 0.3s ease;
            display: flex;
            align-items: flex-start;
            gap: 12px;
        }

        .notification.success {
            border-left-color: #4ecdc4;
        }

        .notification.error {
            border-left-color: #ff6b6b;
        }

        .notification.info {
            border-left-color: #667eea;
        }

        .notification-icon {
            width: 24px;
            height: 24px;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 12px;
            color: white;
            flex-shrink: 0;
            margin-top: 2px;
        }

        .notification-icon.success {
            background: linear-gradient(135deg, #4ecdc4, #44a08d);
        }

        .notification-icon.error {
            background: linear-gradient(135deg, #ff6b6b, #ee5a24);
        }

        .notification-icon.info {
            background: linear-gradient(135deg, #667eea, #764ba2);
        }

        .notification-content {
            flex: 1;
            min-width: 0;
        }

        .notification-title {
            font-size: 14px;
            font-weight: 600;
            color: #2c3e50;
            margin: 0 0 4px 0;
            line-height: 1.3;
        }

        .notification-message {
            font-size: 13px;
            color: #666;
            margin: 0;
            line-height: 1.4;
        }

        .notification-close {
            background: none;
            border: none;
            font-size: 16px;
            color: #999;
            cursor: pointer;
            padding: 2px;
            border-radius: 50%;
            transition: all 0.3s ease;
            flex-shrink: 0;
            margin-top: 2px;
        }

        .notification-close:hover {
            background: #f8f9fa;
            color: #333;
        }

        .notification-progress {
            position: absolute;
            bottom: 0;
            left: 0;
            height: 3px;
            background: rgba(0, 0, 0, 0.1);
            border-radius: 0 0 12px 12px;
            animation: progressShrink 3s linear;
        }

        .notification.success .notification-progress {
            background: linear-gradient(90deg, #4ecdc4, #44a08d);
        }

        .notification.error .notification-progress {
            background: linear-gradient(90deg, #ff6b6b, #ee5a24);
        }

        .notification.info .notification-progress {
            background: linear-gradient(90deg, #667eea, #764ba2);
        }

        @keyframes slideInRight {
            from {
                opacity: 0;
                transform: translateX(100%);
            }
            to {
                opacity: 1;
                transform: translateX(0);
            }
        }

        @keyframes slideOutRight {
            from {
                opacity: 1;
                transform: translateX(0);
            }
            to {
                opacity: 0;
                transform: translateX(100%);
            }
        }

        @keyframes progressShrink {
            from { width: 100%; }
            to { width: 0%; }
        }

        /* 响应式通知 */
        @media (max-width: 480px) {
            .notification-container {
                top: 10px;
                right: 10px;
                left: 10px;
                max-width: none;
            }

            .notification {
                padding: 14px 16px;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <button onclick="window.location.href='index.html'" class="back-btn">
                <i class="fas fa-home"></i> 返回首页
            </button>
            <h1><i class="fas fa-clipboard-list"></i> 粘性初测实验抽样单</h1>
            <div style="width: 100px;"></div>
        </div>

        <!-- 条形码输入区域 -->
        <div class="barcode-input-section">
            <div class="barcode-input-row">
                <span class="barcode-label">条形码内容：</span>
                <input type="text" id="barcodeInput" class="barcode-input" placeholder="请输入21位条形码内容" maxlength="21">
                <button onclick="fillFormFromBarcode()" class="barcode-btn">
                    <i class="fas fa-magic"></i> 填充
                </button>
                <button onclick="generatePrintPreview()" class="barcode-btn">
                    <i class="fas fa-print"></i> 打印
                </button>
            </div>
        </div>

        <!-- 表单区域输入区域 -->
        <div class="form-container">
            <div class="a4-page">
                <div class="title">粘性初测实验抽样单</div>
                <table>
                    <tr>
                        <td colspan="2">机构名称/代码</td>
                        <td colspan="10">
                            <span id="institutionName"></span>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2">抽样人员</td>
                        <td colspan="4"></td>
                        <td colspan="2">抽样日期</td>
                        <td colspan="4"><span id="year"></span>年<span id="month"></span>月<span id="day"></span>日</td>
                    </tr>
                    <tr>
                        <td rowspan="7">抽取批号</td>
                        <td colspan="5" style="min-width: 200px;"></td>
                        <td rowspan="7"  >对应批次抽取的棉包数量</td>
                        <td colspan="5" style="min-width: 300px;"></td>
                    </tr>
                    <tr>
                        <td colspan="5"></td>
                        <td colspan="5"></td>
                    </tr>
                    <tr>
                        <td colspan="5"></td>
                        <td colspan="5"></td>
                    </tr>
                    <tr>
                        <td colspan="5"></td>
                        <td colspan="5"></td>
                    </tr>
                    <tr>
                        <td colspan="5"></td>
                        <td colspan="5"></td>
                    </tr>
                    <tr>
                        <td colspan="5"></td>
                        <td colspan="5"></td>
                    </tr>
                    <tr>
                        <td colspan="5"></td>
                        <td colspan="5"></td>
                    </tr>
                </table>
            </div>
        </div>
    </div>

    <!-- 通知容器 -->
    <div class="notification-container" id="notificationContainer"></div>

    <script>
        // 扫描枪输入处理
        let currentFocusElement = null;

        document.addEventListener('focusin', function(e) {
            if (e.target.classList.contains('scannable')) {
                currentFocusElement = e.target;
                e.target.style.border = '2px solid #ff6600';
                e.target.style.background = '#fff8dc';
            }
        });

        document.addEventListener('focusout', function(e) {
            if (e.target.classList.contains('scannable')) {
                e.target.style.border = '2px solid #ffcc00';
                e.target.style.background = '#ffffcc';
            }
        });

        // 键盘输入处理（模拟扫描枪）
        document.addEventListener('keydown', function(e) {
            if (e.key === 'Enter' && currentFocusElement) {
                e.preventDefault();
                // 处理扫描枪输入的数据
                processScannedData(currentFocusElement.value);
                currentFocusElement.blur();
            }
        });

        // 处理扫描数据
        function processScannedData(data) {
            if (currentFocusElement.id === 'institutionCode') {
                // 机构代码处理
                const unitName = getUnitNameByCode(data);
                if (unitName !== "未知单位") {
                    document.getElementById('institutionName').textContent = unitName;
                }
            }
        }

        // 通知功能
        function showNotification(options = {}) {
            const {
                title = '提示',
                message = '',
                type = 'info', // success, error, info
                duration = 3000 // 自动消失时间，毫秒
            } = options;

            const container = document.getElementById('notificationContainer');
            
            // 创建通知元素
            const notification = document.createElement('div');
            notification.className = `notification ${type}`;
            
            // 图标映射
            const iconMap = {
                success: 'fas fa-check-circle',
                error: 'fas fa-exclamation-circle',
                info: 'fas fa-info-circle'
            };

            // 设置通知内容
            notification.innerHTML = `
                <div class="notification-icon ${type}">
                    <i class="${iconMap[type]}"></i>
                </div>
                <div class="notification-content">
                    <div class="notification-title">${title}</div>
                    <div class="notification-message">${message}</div>
                </div>
                <button class="notification-close" onclick="removeNotification(this.parentElement)">
                    <i class="fas fa-times"></i>
                </button>
                <div class="notification-progress"></div>
            `;

            // 添加到容器
            container.appendChild(notification);

            // 自动移除
            setTimeout(() => {
                removeNotification(notification);
            }, duration);

            return notification;
        }

        function removeNotification(notification) {
            if (notification && notification.parentElement) {
                notification.style.animation = 'slideOutRight 0.3s ease';
                setTimeout(() => {
                    if (notification.parentElement) {
                        notification.parentElement.removeChild(notification);
                    }
                }, 300);
            }
        }

        // 清理所有通知
        function clearAllNotifications() {
            const container = document.getElementById('notificationContainer');
            container.innerHTML = '';
        }

        // 从条形码填充表单
        function fillFormFromBarcode() {
            const barcodeInput = document.getElementById('barcodeInput');
            const barcodeContent = barcodeInput.value.trim();
            
            if (!barcodeContent) {
                showNotification({
                    title: '提示',
                    message: '请输入条形码内容',
                    type: 'error'
                });
                return;
            }
            
            if (barcodeContent.length !== 21) {
                showNotification({
                    title: '格式错误',
                    message: '条形码长度必须为21位',
                    type: 'error'
                });
                return;
            }
            
            const parsedData = parseBarcode(barcodeContent.slice(-19));
            if (!parsedData) {
                showNotification({
                    title: '解析失败',
                    message: '条形码格式错误，无法解析',
                    type: 'error'
                });
                return;
            }
            console.log(parsedData);
            // 填充机构名称和代码
            document.getElementById('institutionName').textContent = parsedData.sendingUnitName;
            
            // 填充日期
            document.getElementById('year').textContent = parsedData.year;
            document.getElementById('month').textContent = parsedData.month;
            document.getElementById('day').textContent = parsedData.day;
            
            showNotification({
                title: '填充成功',
                message: '表单已根据条形码内容自动填充',
                type: 'success'
            });
        };

        // 键盘事件处理（支持回车键填充）
        document.getElementById('barcodeInput').addEventListener('keypress', function(e) {
            if (e.key === 'Enter') {
                fillFormFromBarcode();
            }
        });

        // 打印功能
        function generatePrintPreview() {
            showNotification({
                title: '打印预览',
                message: '正在打开打印预览窗口...',
                type: 'info'
            });
            const formContainer = document.querySelector('.form-container');
            
            if (!formContainer) {
                showNotification({
                    title: '打印错误',
                    message: '未找到要打印的内容',
                    type: 'error'
                });
                return;
            }

            // 创建打印样式
            const printStyle = document.createElement('style');
            printStyle.id = 'print-style';
            printStyle.innerHTML = '@media print { @page { size: A4 landscape; margin: 8mm; } body * { visibility: hidden; } .form-container, .form-container * { visibility: visible; } .form-container { position: absolute; left: 0; top: 0; width: 100%; height: 100%; margin: 0; padding: 0; background: white; } .form-container .a4-page { padding: 8mm; margin: 0; } .form-container .title { font-size: 25px; margin-bottom: 15px; } .form-container table { width: 100%; height: 500px!important; } .form-container td { padding: 10px; min-height: 70px; font-size: 18px; } .no-print { display: none !important; } table { page-break-inside: avoid; } }';
            
            // 添加打印样式
            document.head.appendChild(printStyle);
            
            // 执行打印
            window.print();
            
            // 打印完成后移除样式
            setTimeout(() => {
                if (document.getElementById('print-style')) {
                    document.head.removeChild(document.getElementById('print-style'));
                }
            }, 1000);
        }


    </script>
</body>
</html>